<?php
  require('includes/application_top.php');
  
  function delete_directory($dirname) {
    if (is_dir($dirname))
      $dir_handle = opendir($dirname);
    if (!$dir_handle)
      return false;
    while($file = readdir($dir_handle)) {
      if ($file != "." && $file != "..") {
         if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
        else
           delete_directory($dirname.'/'.$file);    
     }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}  

  
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  if (tep_not_null($action)) {
    switch ($action) {
        case 'change':           
            $change_templates = $_REQUEST['change_templates'];
            if($change_templates){
                tep_db_query("UPDATE ushop_templates SET default_templates=0");
                tep_db_query("UPDATE ushop_templates SET default_templates=1 WHERE id=$change_templates");                
            }
    
            tep_redirect(tep_href_link('templates.php'));
        break;
        
        case 'delete':
            $id = tep_db_prepare_input($HTTP_GET_VARS['tID']);
            
            $folders_query = tep_db_query("select folders from ushop_templates where id = '" . (int)$id . "'");
            $folders = tep_db_fetch_array($folders_query);
            
            delete_directory($folders['folders']);
            
            tep_db_query("delete from ushop_templates where id = '" . (int)$id . "'");
    
            tep_redirect(tep_href_link('templates.php'));
        break;    
    }
  } 
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/general.js"></script>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onload="SetFocus();">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top">
    <table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td valign="top">

Danh sách các Templates:
<br />Chọn Templates chính
<form name="template" action="templates.php?action=change" method="post">
    <select name="change_templates" onChange="document.template.submit()">
        <option value="">Chọn Templates</option>
        <?php
            $templates_query_option = tep_db_query("select * from ushop_templates order by name");
            while ($templates_option = tep_db_fetch_array($templates_query_option)) {
        ?>        
            <option value="<?php echo $templates_option['id']; ?>"><?php echo $templates_option['name']; ?></option>        
        <?php } ?>
    </select>
</form>
<br /><br />
<form name="widgets_re" action="" method="post">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-size: 12px;">
    <tr bgcolor="#B3BAC5" style="font-weight: bold;">
        <td>Tên</td>
        <td>Mô tả</td>
        <td>Mặc định</td>
        <td></td>
    </tr>
    <tr>
<?php 
    $templates_query_raw = "select * from ushop_templates order by name";
    $templates_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $templates_query_raw, $templates_query_numrows);
    $templates_query = tep_db_query($templates_query_raw);
    while ($templates = tep_db_fetch_array($templates_query)) {
        echo '<td>'.$templates['name'].'</td>';
        echo '<td>'.$templates['description'].'</td>';
        if($templates['default_templates'] == 0){
            echo '<td></td>';
        }else{
            echo '<td>Mặc định</td>';    
        }
        echo '<td><a href="' . tep_href_link('templates.php', 'tID=' . $templates['id'] . '&action=delete') . '">Xóa</a></td>';
        
        echo '</tr>';   
    }
?>
        <tr>
            <td class="smallText" valign="top" align="right" colspan="6">
                <?php echo $templates_split->display_count($templates_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_ORDERS); ?>
                <?php echo $templates_split->display_links($templates_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page']); ?>
            </td>
        </tr>
</form>
<tr><td style="padding-bottom: 20px;"></td></tr>    
</table>

    </td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
